var http = require('http'),
    util = require('util'),
    mime = require('mime'),
    fs = require('fs'),
    formidable = require('formidable');
exports.execute = function(req,res,root){
	var form = new formidable.IncomingForm(),
        files = [],
        fields = [];

    form.uploadDir = root+"/_upload/";

    form.on('field', function(field, value) {
			fields.push({name: field, value: value});
		})
		.on('file', function(field, file) {
			files.push({name: field, file:file});
		})
		.on('end', function() {
			for (var i = 0; i < files.length; i++) {
				var f = files[i].file;
				fs.rename(f.path, form.uploadDir+f.name, function (err) {
					if (err) throw err;
				});
			};
			res.writeHead(200, {'content-type': mime.get("json")});
			console.log( fields );
			res.end(util.inspect(files));
		});
    form.parse(req);
}